package com.han.shadow.customerapp.http;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;


import com.han.shadow.customerapp.bean.Account;
import com.han.shadow.customerapp.bean.Business;
import com.han.shadow.customerapp.bean.Goods;
import com.han.shadow.customerapp.bean.Order;
import com.han.shadow.customerapp.inter.ICustomer;
import com.han.shadow.customerapp.json.JsonUtil;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ACE on 2016/4/20 0020.
 */

public class Connector implements ICustomer {


    //private String ip = "192.168.1.101";
    private String ip = "115.159.220.253";

    @Override
    public int addToCart(String goodsID) {
        return 0;
    }

    @Override
    public int deleteFromCart(String goodsID) {
        return 0;
    }

    @Override
    public int queryCart() {
        return 0;
    }



    public String login(String acc,String psd){

        String url = "http://"+ip+":8080/StuMs/customerLogin?account="+acc+"&psd="+psd;

        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(response.getStatusLine().getStatusCode() != 500){
            try {
                String cid = EntityUtils.toString(response.getEntity());
                return cid;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }


    @Override
    public List<Business> queryBusiness(String bID) {

        String url = "http://"+ip+":8080/StuMs/queryBusiness";
        if(bID != null){
            url += "?bid=";
            url += bID;
        }
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        List<Business> businesses = null;
        try {
            HttpResponse response = httpClient.execute(httpGet);
            JsonUtil jsonUtil = new JsonUtil();
            try {
                HttpEntity entity = response.getEntity();
                String str = EntityUtils.toString(entity,HTTP.UTF_8);
                Log.i("dddd",str);
                businesses = jsonUtil.JSON2listBusiness(str);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return businesses;
    }

    @Override
    public List<Goods> queryGoods(String gID,String bID) {
        String url = "";

        if(gID != null) {
            url  = "http://" + ip + ":8080/StuMs/queryGoods?gid=" + gID;
        }else if(bID != null){
            url = "http://" + ip + ":8080/StuMs/queryGoods?bid=" + bID;
        }
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        List<Goods> goodses = null;
        try {
            HttpResponse response = httpClient.execute(httpGet);
            JsonUtil jsonUtil = new JsonUtil();
            try {
                HttpEntity entity = response.getEntity();
                String str = EntityUtils.toString(entity);

                goodses = jsonUtil.JSON2listGoods(str);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return goodses;
    }

    @Override
    public int checkout(Order order1) {


        String url = "http://"+ip+":8080/StuMs/order";
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost  = new HttpPost(url);
        List<NameValuePair> postParameters = new ArrayList<>();

        JsonUtil jsonUtil = new JsonUtil();
        List<Order> orders = new ArrayList<>();
        orders.add(order1);
        JSONArray ja = null;
        try {
            ja = jsonUtil.listOrder2Json(orders);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            //se = new StringEntity(jo.toString());

            postParameters.add(new BasicNameValuePair("json", ja
                    .toString()));

            httpPost.setEntity(new UrlEncodedFormEntity(postParameters, HTTP.UTF_8));

            HttpResponse response = httpClient.execute(httpPost); //执行POST方法

        } catch (IOException e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public int cancelOrder(String orderID) {
        return 0;
    }

    @Override
    public List<Order> queryOrders(String bID) {
        String url = "http://"+ip+":8080/StuMs/queryCustomerOrders?cid="+bID;

        List<Order> orders = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            JsonUtil jsonUtil = new JsonUtil();
            HttpEntity entity = response.getEntity();
            String str = EntityUtils.toString(entity);

            orders = jsonUtil.Json2ListOrder(str);
        } catch (IOException e) {
            e.printStackTrace();
        }catch (JSONException e) {
            e.printStackTrace();
        }
        return orders;
    }

    @Override
    public int deleteOrder(String orderID) {
        return 0;
    }

    @Override
    public int signUp(Account acc) {
        return 0;
    }

    public Bitmap queryPic(String pic) {

        String url = "http://" + ip + ":8080/StuMs/queryGoodsPic?pic=" + pic;

        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        Bitmap bm = null;

        try {
            HttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            String str = EntityUtils.toString(entity);

            byte[] bs = str.getBytes("ISO-8859-1");
            bm = BitmapFactory.decodeByteArray(bs,0,bs.length);



//            File file = new File("/mnt/sdcard/a.png");
//            FileOutputStream outputStream;
//            try {
//                outputStream = new FileOutputStream(file);
//                outputStream.write(bs);
//                outputStream.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bm;
    }


}
